<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');
session_start();

$action = $_REQUEST['FORM_SUBMIT'];

$cart	= new shoppingCart();

switch($action){
	case "addtocart":
		$cart->addToCart();
		break;
	case "updatecart":
		$cart->updateCart();
		break;
	case "removefromcart":
		$cart->removeFromCart();
		break;		
}


class shoppingCart
{
	
	public function addToCart()
	{
		$id			= $_POST['id'];
		$skuid		= $_POST['skuid'];
		$name		= $_POST['prodcut_name'];
		$price		= $_POST['price'];
		$colour		= $_POST['colour'];
		$size		= $_POST['size'];	
		$quantity	= $_POST['quantity'];
		$colour_ids	= $_POST['colour_ids'];
		$size_ids	= $_POST['size_ids'];		
		
		$sessionID	= $id."_".$size."_".trim(strtolower($colour));
		
		if(isset($_SESSION['cart'][$sessionID])){
			$_SESSION['cart'][$sessionID]['quantity'] += $quantity;
		}else{
			$_SESSION['cart'][$sessionID] = array(
				"id"		=> $id,
				"skuid"		=> $skuid,
				"name"		=> $name,
				"price"		=> $price,
				"colour"	=> $colour,
				"size"		=> $size,
				"quantity"	=> $quantity,
				"colour_ids"=> $colour_ids,
				"size_ids"	=> $size_ids
			);		
		}
	}
	
	public function updateCart()
	{
		$id				= $_POST['id'];
		$old_sessionID	= $_POST['session_id'];
		$colour		= $_POST['colour'];
		$size		= $_POST['size'];	
		$quantity	= $_POST['quantity'];	
		
		$sessionID	= $id."_".$size."_".trim(strtolower($colour));
		
		if($sessionID == $old_sessionID){
			$_SESSION['cart'][$sessionID]['quantity'] = $quantity;
		}else{
			if(isset($_SESSION['cart'][$sessionID])){
				$_SESSION['cart'][$sessionID]['quantity'] += $_SESSION['cart'][$old_sessionID]['quantity'];
			}else{
				$_SESSION['cart'][$sessionID] = $_SESSION['cart'][$old_sessionID];
				$_SESSION['cart'][$sessionID]['colour'] = $colour;
				$_SESSION['cart'][$sessionID]['size'] = $size;
				$_SESSION['cart'][$sessionID]['quantity'] = $quantity;
			}
			unset($_SESSION['cart'][$old_sessionID]);		
		}
		
		header("location:shoppingcart.html");exit;
	}	
	
	public function removeFromCart()
	{
		$sessionID = $_GET['sessionID'];
		unset($_SESSION['cart'][$sessionID]);		
		header("location:shoppingcart.html");exit;
	}	
	
}
?>